import pymysql
class DBUtil(object):
    conn = None

    @classmethod
    def __get_conn(cls):
        if cls.conn is None:
            cls.conn = pymysql.connect(host="211.103.136.244", port=7061, user="student",
                                                 password="iHRM_student_2021",database="ihrm", charset="utf8")
            return cls.conn

    @classmethod
    def __close_conn(cls):
        if cls.conn:
            cls.conn.close()
        cls.conn = None

    @classmethod
    def select_one(cls, sql):
        cursor = None
        result = None
        try:
            cls.__get_conn()
            cursor = cls.conn.cursor()
            cursor.execute(sql)
            result = cursor.fetchone()
        except Exception as e: print(e)
        finally:
            if cursor:
                cursor.close()
            cls.__close_conn()
            return result

    @classmethod
    def uid_db(cls, sql):
        cursor = None
        try:
            cls.__get_conn()
            cursor = cls.conn.cursor()
            cursor.execute(sql)
            cls.conn.commit()
        except Exception as e:
            print(e)
            cls.conn.rollback()
        finally:
            if cursor: cursor.close()
            cls.__close_conn()